﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;

namespace WaiGaoQiaoWeb.Controllers
{
    public class GuestNumberInventoryController : Controller
    {
        public ActionResult GuestNumberInventoryIndex()
        {
            ViewData["yy"] = GetOutstockYY();
            ViewData["mm"] = GetOutstockMM();
            ViewData["canteen"] = GetGroup();
            return View();
        }

        /// <summary>
        /// 获取年份列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetOutstockYY()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(1, (DateTime.Now.Date.Year - 1).ToString());
            dic.Add(2, (DateTime.Now.Date.Year).ToString());
            dic.Add(3, (DateTime.Now.Date.Year + 1).ToString());
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem()
                {
                    Text = m.Value.ToString(),
                    Value = m.Key.ToString(),
                    Selected = (m.Value == (DateTime.Now.Date.Year).ToString()) ? true : false
                }).ToList();
            return selectList;
        }
        /// <summary>
        /// 获取月份列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetOutstockMM()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(1, "1");
            dic.Add(2, "2");
            dic.Add(3, "3");
            dic.Add(4, "4");
            dic.Add(5, "5");
            dic.Add(6, "6");
            dic.Add(7, "7");
            dic.Add(8, "8");
            dic.Add(9, "9");
            dic.Add(10, "10");
            dic.Add(11, "11");
            dic.Add(12, "12");
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem()
                {
                    Text = m.Value.ToString(),
                    Value = m.Key.ToString(),
                    Selected = (m.Value == DateTime.Now.Month.ToString()) ? true : false
                }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取食堂列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetYaxNode()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            YaxNodeInfo yaxinfo = new YaxNodeInfo(true);
            IList<YaxNodeInfo> yaxlist = yaxinfo.SelYaxNodeInfo("and CONFLAG=1");
            foreach (YaxNodeInfo yaxnode in yaxlist)
            {
                dic.Add(yaxnode.NodeID, yaxnode.Nodename);
            }
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取班组列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetGroup()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            GuestNumberInventoryInfo yaxinfo = new GuestNumberInventoryInfo();
            DataSet ds = yaxinfo.GetGroup();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                dic.Add(Convert.ToInt32(dr["ID"]), dr["Group_Name"].ToString());
            }
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 报表导出
        /// </summary>
        /// <returns></returns>
        public ActionResult StaffCanteenWarehouseOut(string yy, string mm, string yax, string yaxname)
        {
            return EContent(yy, mm, yax, yaxname);
        }

        public ExcelContentResult EContent(string yy, string mm, string yax, string yaxname)
        {
            StringBuilder sBuilder = new StringBuilder();
            sBuilder.Append("<table cellspacing='0' cellpadding='5' rules='all' border='1'>");
            sBuilder.Append("<tr><td style='font-size:25px;'>餐饮部" + yaxname + "客数清单</td></tr>");
            sBuilder.Append("<tr><td style='font-size:17px;'>日期:" + yy + "年" + (Convert.ToInt32(mm) - 1).ToString() + "月21日至" + mm + "月20日</td></tr>");
            sBuilder.Append("<tr><td>");
            sBuilder.Append("<table cellspacing='0' cellpadding='5' rules='all' border='1' style='border:border-width;'>");
            sBuilder.Append("<tr style='font-size: 17px;'>");
            sBuilder.Append("<td>日期</td>");
            sBuilder.Append("<td>现场清单</td>");
            sBuilder.Append("<td>拉卡客数</td>");
            sBuilder.Append("<td>外卖就餐券</td>");
            sBuilder.Append("<td>客饭就餐券</td>");
            sBuilder.Append("<td>外理</td>");
            sBuilder.Append("<td>小计</td>");
            sBuilder.Append("</tr>");
            GuestNumberInventoryInfo yaxinfo = new GuestNumberInventoryInfo();
            DataSet ds = yaxinfo.GetList(yy, mm, yax);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (ds.Tables[0].Rows[i]["日期"].ToString() == "合计")
                {
                    sBuilder.Append("<tr style='font-size: 14px;'>");
                    sBuilder.Append("<td style='font-weight: bold'>" + ds.Tables[0].Rows[i]["日期"] + "</td>");
                    sBuilder.Append("<td>" + ds.Tables[0].Rows[i]["现场清单数量"] + "(" + ds.Tables[0].Rows[i]["现场清单"] + ")</td>");
                    sBuilder.Append("<td>" + ds.Tables[0].Rows[i]["拉卡数量"] + "(" + ds.Tables[0].Rows[i]["拉卡"] + ")</td>");
                    sBuilder.Append("<td>" + ds.Tables[0].Rows[i]["外卖就餐券数量"] + "(" + ds.Tables[0].Rows[i]["外卖就餐券"] + ")</td>");
                    sBuilder.Append("<td>" + ds.Tables[0].Rows[i]["客饭就餐券数量"] + "(" + ds.Tables[0].Rows[i]["客饭就餐券"] + ")</td>");
                    sBuilder.Append("<td>" + ds.Tables[0].Rows[i]["外理数量"] + "(" + ds.Tables[0].Rows[i]["外理"] + ")</td>");
                    sBuilder.Append("<td>" + ds.Tables[0].Rows[i]["小计数量"] + "(" + ds.Tables[0].Rows[i]["小计"] + ")</td>");
                    sBuilder.Append("</tr>");
                    break;
                }
                sBuilder.Append("<tr style='font-size: 14px;'>");
                sBuilder.Append("<td>" + ds.Tables[0].Rows[i]["日期"] + "</td>");
                sBuilder.Append("<td>" + ds.Tables[0].Rows[i]["现场清单数量"] + "(" + ds.Tables[0].Rows[i]["现场清单"] + ")</td>");
                sBuilder.Append("<td>" + ds.Tables[0].Rows[i]["拉卡数量"] + "(" + ds.Tables[0].Rows[i]["拉卡"] + ")</td>");
                sBuilder.Append("<td>" + ds.Tables[0].Rows[i]["外卖就餐券数量"] + "(" + ds.Tables[0].Rows[i]["外卖就餐券"] + ")</td>");
                sBuilder.Append("<td>" + ds.Tables[0].Rows[i]["客饭就餐券数量"] + "(" + ds.Tables[0].Rows[i]["客饭就餐券"] + ")</td>");
                sBuilder.Append("<td>" + ds.Tables[0].Rows[i]["外理数量"] + "(" + ds.Tables[0].Rows[i]["外理"] + ")</td>");
                sBuilder.Append("<td>" + ds.Tables[0].Rows[i]["小计数量"] + "(" + ds.Tables[0].Rows[i]["小计"] + ")</td>");
                sBuilder.Append("</tr>");
            }
            sBuilder.Append("<tr style='font-size: 14px;'>");
            sBuilder.Append("<td colspan='7'>备注:员工食堂半夜饭就餐&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;客</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size: 14px;'>");
            sBuilder.Append("<td colspan='7'>制表:</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("</table>");
            sBuilder.Append("</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("</table>");
            ExcelContentResult econtent = new ExcelContentResult();
            econtent.Content = sBuilder.ToString();
            econtent.filename = "StaffCanteenWarehouseOut1";
            econtent.ContentType = "application/ms-excel";
            econtent.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            return econtent;
        }
    }
}
